library(ggplot2)
library(patchwork)

# Study Models ------------------------------------------------------------
setwd("./Code/")
source("./main/study2_sems.R")
source("./appendix/study3_sems_attentive.R")
source("./appendix/study4_sems_attentive.R")

# Combine Studies ---------------------------------------------------------
ests_wide <- rbind(ests_wide_s2,
                   ests_wide_s3,
                   ests_wide_s4)

ests_wide$study <- factor(ests_wide$study,
                          labels = c("Study 2 (Dynata)", "Study 3 (MTurk)", "Study 4 (Bovitz)"))


# Figure A5 ---------------------------------------------------------------
ggplot(data = subset(ests_plot, method == "Constructs Alone"), 
       aes(x = `est.std.No Method`, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 3, aes(shape = study)) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  # facet_grid(~method) +
  labs(x = "Correlation without Method Factor", y = "Correlation with Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 14),
        legend.title = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())
# ggsave("FigureA5", width = 6, height = 4)

# Summary Stats -----------------------------------------------------------
# Proportion of corrections reducing correlation
prop.table(table(ests_plot$`est.std.No Method`[which(ests_plot$method == "Constructs Alone")] >
                   ests_plot$est.std.Method[which(ests_plot$method == "Constructs Alone")]))

## Average Correlation Differences
summary(abs(ests_plot$`est.std.No Method`[which(ests_plot$method == "Constructs Alone")] - 
              ests_plot$est.std.Method[which(ests_plot$method == "Constructs Alone")]))

# S3
prop.table(table(ests_wide_s3$`est.std.No Method` < ests_wide_s3$est.std.Method))

# S4
prop.table(table(ests_wide_s4$`est.std.No Method` < ests_wide_s4$est.std.Method))


# Scales with Largest Gaps ------------------------------------------------
ests_wide$corr_diff <- abs(ests_wide$`est.std.No Method` - ests_wide$est.std.Method)

# violence among more stable, RR, pop, and HS not
cd_hs <- subset(ests_wide, rhs == "hs" | lhs == "hs", select = c("lhs", "rhs", "corr_diff", "study", "est.std.No Method", "est.std.Method"))
cd_hs[which(cd_hs$lhs == "rr"), ]


# Figure A6 ---------------------------------------------------------------
obvar_corrs <- read.csv("../Data/naive_corr_labeled.csv")

obvar_corrs$alignrev <- factor(obvar_corrs$alignrev,
                               levels = c(0, 1),
                               labels = c("PW-PW", "NW-NW"))

obvar_corrs <- obvar_corrs[which(duplicated(obvar_corrs) == FALSE), ]

obvar_corrs$study <- paste("Study", obvar_corrs$study)

ests_wide$pair <- paste(ests_wide$lhs, ests_wide$rhs, sep = "-")
ests_wide$study2 <- NA
ests_wide$study2[which(ests_wide$study == "Study 2 (Dynata)")] <- "Study 2"
ests_wide$study2[which(ests_wide$study == "Study 3 (MTurk)")] <- "Study 3"
ests_wide$study2[which(ests_wide$study == "Study 4 (Bovitz)")] <- "Study 4"

corr_compare <- merge(ests_wide, obvar_corrs, by.x = c("study2", "pair"), by.y = c("study", "pair"))

corr_compare$corr_diff <- corr_compare$est.std.Method-corr_compare$cor1

# Plot
p_all_corrs <- ggplot(data = corr_compare, aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 3, aes(shape = study)) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  labs(x = "Correlation using Observed Index", y = "Correlation using CFA and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

# Descriptives
mean(abs(corr_compare$corr_diff))
summary(abs(corr_compare$corr_diff))

prop.table(table(corr_compare$corr_diff > 0))

mean(abs(corr_compare$cor1))
mean(abs(corr_compare$est.std.Method))

# Plot
p_direction <- ggplot(data = corr_compare, aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 3, aes(shape = study)) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  labs(x = "Correlation using Observed Index", y = "Correlation using CFA and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 14),
        strip.background = element_blank())

(p_all_corrs / p_direction) &
  labs(x = "Correlation using Observed Index",
       y = "Correlation using CFA\nand Method Factor") &
  scale_x_continuous(limits = c(-0.15, .80)) &
  scale_y_continuous(limits = c(-0.15, .80)) &
  plot_annotation(tag_levels = "A") &
  theme(axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.text = element_text(size = 12))
# ggsave("FigureA6.pdf", width = 6, height = 6)

